﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tesla.Service.WcfServiceCommon;
using System.ServiceModel;
using Tesla.Service.ServiceContracts;
using Tesla.Service.DataContracts;
using Tesla.Util.ValidateEntity;


namespace Tesla.Web
{
    public class ClientServiceDispatcher
    {
        public IList<BaseDC> DisplayAllClient(int gridSize, ref int? totalRecordCount)
        {

            ISvcClient _proxySvcClient;
            ChannelFactory<ISvcClient> chfClient = null; IList<BaseDC> Clients = null;

            try
            {
                chfClient = GADNChannelFactoryProvider.CreateChannelFactory<ISvcClient>();
                _proxySvcClient = chfClient.CreateChannel();
                Clients = _proxySvcClient.DisplayAll(gridSize, ref totalRecordCount);
            }
            catch (FaultException gfx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfClient.Close();
            }
            return Clients;
        }



        public IList<BaseDC> SearchClientByName(string name, int gridSize, ref int? totalRecordCount)
        {
            ISvcClient _proxySvcClient;
            ChannelFactory<ISvcClient> chfClient = null; IList<BaseDC> Clients = null;

            try
            {
                chfClient = GADNChannelFactoryProvider.CreateChannelFactory<ISvcClient>();
                _proxySvcClient = chfClient.CreateChannel();
                Clients = _proxySvcClient.SearchByName(name, gridSize, ref totalRecordCount);
            }
            catch (FaultException gfx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfClient.Close();
            }
            return Clients;
        }

        public IList<BaseDC> ReadClient(int id)
        {
            ISvcClient _proxySvcClient;
            ChannelFactory<ISvcClient> chfClient = null; IList<BaseDC> Clients = null;

            try
            {
                chfClient = GADNChannelFactoryProvider.CreateChannelFactory<ISvcClient>();
                _proxySvcClient = chfClient.CreateChannel();
                Clients = _proxySvcClient.Read(id);
            }
            catch (FaultException gfx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfClient.Close();
            }
            return Clients;
        }

        public IList<BaseDC> ReadAllClients()
        {
            ISvcClient _proxySvcClient;
            ChannelFactory<ISvcClient> chfClient = null; IList<BaseDC> Clients = null;

            try
            {
                chfClient = GADNChannelFactoryProvider.CreateChannelFactory<ISvcClient>();
                _proxySvcClient = chfClient.CreateChannel();
                Clients = _proxySvcClient.ReadAllClients();
            }
            catch (FaultException gfx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfClient.Close();
            }
            return Clients;
        }

        public IList<BaseDC> ReadProjectsForClient(int clientID)
        {
            ISvcClient _proxySvcClient;
            ChannelFactory<ISvcClient> chfClient = null; IList<BaseDC> projects = null;

            try
            {
                chfClient = GADNChannelFactoryProvider.CreateChannelFactory<ISvcClient>();
                _proxySvcClient = chfClient.CreateChannel();
                projects = _proxySvcClient.ReadProjectsForClient(clientID);
            }
            catch (FaultException gfx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfClient.Close();
            }
            return projects;
        }

        public IList<BaseDC> ReadAllOperatingUnits()
        {
            ISvcClient _proxySvcClient;
            ChannelFactory<ISvcClient> chfClient = null; IList<BaseDC> OUs = null;

            try
            {
                chfClient = GADNChannelFactoryProvider.CreateChannelFactory<ISvcClient>();
                _proxySvcClient = chfClient.CreateChannel();
                OUs = _proxySvcClient.ReadAllOperatingUnits();
            }
            catch (FaultException gfx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfClient.Close();
            }
            return OUs;
        }

        public IList<BaseDC> ReadClientByRowNumberRange(int startRecordNo, int endRecordNo, string sortColName, string sortDirection, string searchString)
        {
            ISvcClient _proxySvcClient;
            ChannelFactory<ISvcClient> chfClient = null; IList<BaseDC> Clients = null;

            try
            {
                chfClient = GADNChannelFactoryProvider.CreateChannelFactory<ISvcClient>();
                _proxySvcClient = chfClient.CreateChannel();
                Clients = _proxySvcClient.ReadByRange(startRecordNo, endRecordNo, sortColName, sortDirection, searchString);
            }
            catch (FaultException gfx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfClient.Close();
            }
            return Clients;
        }


        public int? CreateClient(BaseDC bdc)
        {
            ISvcClient _proxySvcClient;
            ChannelFactory<ISvcClient> chfClient = null; int? success = 0;

            try
            {
                chfClient = GADNChannelFactoryProvider.CreateChannelFactory<ISvcClient>();
                _proxySvcClient = chfClient.CreateChannel();
                success = _proxySvcClient.Create(bdc);
            }
            catch (FaultException gfx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
                throw gfx;
            }
            catch (TimeoutException tx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfClient.Close();
            }
            return success;
        }

        public int UpdateClient(BaseDC bdc)
        {
            ISvcClient _proxySvcClient;
            ChannelFactory<ISvcClient> chfClient = null; int updateResult = 0;

            try
            {
                chfClient = GADNChannelFactoryProvider.CreateChannelFactory<ISvcClient>();
                _proxySvcClient = chfClient.CreateChannel();
                updateResult = _proxySvcClient.Update(bdc);
            }
            catch (FaultException gfx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
                throw gfx;
            }
            catch (TimeoutException tx)
            {
                if (chfClient.State == CommunicationState.Faulted || chfClient.State == CommunicationState.Opened)
                    chfClient.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfClient.Close();
            }
            return updateResult;
        }
    }
}

